﻿using MyLibrary.Function;
using Prism.Commands;
using Prism.Mvvm;
using Serial_Communication.Common.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Serial_Communication.ViewModels
{
    public class TransitionViewModel : BindableBase
    {
        //创建一个委托命令
        public DelegateCommand<string> ExecuteCommand { get; private set; }

        public TransitionViewModel()
        {
            TransitionDtos = new TransitionDtos();
            ExecuteCommand = new DelegateCommand<string>(Execute);
        }

        //委托命令执行事件
        private void Execute(string obj)
        {
            switch (obj)
            {
                case "CRC16Check": CRC16Check(); break;
                case "XORCheck": XORCheck(); break;
                case "FCSCheck": FCSCheck(); break;
                case "CheckSumCheck": CheckSumCheck(); break;
                case "LRCCheck": LRCCheck(); break;
            }
        }

        private TransitionDtos transitionDtos;

        public TransitionDtos TransitionDtos
        {
            get { return transitionDtos; }
            set { transitionDtos = value; RaisePropertyChanged(); }
        }

        public void CRC16Check()
        {
            TransitionDtos.textCRC16Check = CommunicationCheck.CRC16ModbusCheck(TransitionDtos.textInputString);
            TransitionDtos.textOutputString = DataConvert.ToHexByteString(TransitionDtos.textInputString) + TransitionDtos.textCRC16Check;
        }

        public void XORCheck()
        {
            TransitionDtos.textXORCheck = CommunicationCheck.XORCheck(TransitionDtos.textInputString);
            TransitionDtos.textOutputString = DataConvert.ToHexByteString(TransitionDtos.textInputString) + TransitionDtos.textXORCheck;
        }

        public void FCSCheck()
        {
            TransitionDtos.textFCSCheck = CommunicationCheck.FCSCheck(TransitionDtos.textInputString);
            TransitionDtos.textOutputString = DataConvert.ToHexByteString(TransitionDtos.textInputString) + TransitionDtos.textFCSCheck;
        }

        public void CheckSumCheck()
        {
            TransitionDtos.textCheckSumCheck = CommunicationCheck.CheckSumCheck(TransitionDtos.textInputString);
            transitionDtos.textOutputString = DataConvert.ToHexByteString(TransitionDtos.textInputString) + TransitionDtos.textCheckSumCheck;
        }

        public void LRCCheck()
        {
            TransitionDtos.textLRCCheck = CommunicationCheck.LRCCheck(TransitionDtos.textInputString);
            transitionDtos.textOutputString = DataConvert.ToHexByteString(TransitionDtos.textInputString) + TransitionDtos.textLRCCheck;
        }
    }
}
